package cn.com.shadowless.xinning.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mengpeng.mphelper.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.adapter.ApprovalAdapter;
import cn.com.shadowless.xinning.adapter.ContractReviewAdapter;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.ApprovalInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;
import cn.com.shadowless.xinning.view.activity.OfficeActivity;

/**
 * The type Approval fragment.
 *
 * @author sHadowLess
 */
public class ApprovalFragment extends BaseFragment {

    /**
     * The Approval order num.
     */
    @BindView(R.id.approval_orderNum)
    TextView approvalOrderNum;

    /**
     * The Approval order date.
     */
    @BindView(R.id.approval_orderDate)
    TextView approvalOrderDate;

    /**
     * The Approval maker.
     */
    @BindView(R.id.approval_maker)
    TextView approvalMaker;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_reviewer)
    TextView approvalReviewer;

    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_department)
    TextView approvalDepartment;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_tel)
    TextView approvalTel;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_date)
    TextView approvalDate;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_title)
    TextView approvalTitle;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_isOut)
    TextView approvalIsOut;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_content)
    TextView approvalContent;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_SingleReason)
    TextView approvalSingleReason;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_SingleAmount)
    TextView approvalSingleAmount;
    /**
     * The Approval reviewer.
     */
    @BindView(R.id.approval_ExpectedDate)
    TextView approvalExpectedDate;
    /**
     * The Contract review attach info.
     */
    @BindView(R.id.approval_attachInfo)
    RecyclerView approvalAttachInfo;

    /**
     * The Approval temp item.
     */
    @BindView(R.id.approval_temp_item)
    LinearLayout approvalTempItem;
    /**
     * The Contract review check process.
     */
    @BindView(R.id.approval_check_process)
    LinearLayout approvalCheckProcess;

    private final String url = "api/ApprovalAppForm/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_approval_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initApprovalInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, approvalCheckProcess);
        }
    }

    @Override
    protected void initView() {

    }

    private void initApprovalInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getApprovalInfo(orderId, userName, new RequestCallBack<ApprovalInfo>() {
                    @Override
                    public void success(RequestResult<ApprovalInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.getResult().toString());
                                ApprovalInfo.DataDTO dataBean = result.getResult().getData();
                                List<ApprovalInfo.DataDTO.FileListDTO> fileList = dataBean.getFileList();
                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                String applicantName = dataBean.getApplicantName();
                                String departmentName = dataBean.getDepartmentName();
                                String telephone = dataBean.getTelephone();
                                String applyDateStr = dataBean.getApplyDateStr();
                                String title = dataBean.getTitle();
                                boolean isTemp = dataBean.isTemp();
                                String remark = dataBean.getRemark();

                                approvalOrderNum.setText(id);
                                approvalOrderDate.setText(createTimeStr);
                                approvalMaker.setText(createUserName);
                                approvalReviewer.setText(applicantName);
                                approvalDepartment.setText(departmentName);
                                approvalTel.setText(telephone);
                                approvalDate.setText(applyDateStr);
                                approvalTitle.setText(title);
                                if (isTemp) {
                                    approvalIsOut.setText("是");
                                    approvalTempItem.setVisibility(View.VISIBLE);
                                    String tempReason = dataBean.getTempReason();
                                    String tempCost = getDecimal(dataBean.getTempCost().doubleValue());
                                    String returnDateStr = dataBean.getReturnDateStr();
                                    approvalSingleReason.setText(tempReason);
                                    approvalSingleAmount.setText(tempCost);
                                    approvalExpectedDate.setText(returnDateStr);
                                } else {
                                    approvalIsOut.setText("否");
                                    approvalTempItem.setVisibility(View.GONE);
                                }
                                approvalContent.setText(remark);

                                approvalAttachInfo.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
                                ApprovalAdapter approvalAdapter = new ApprovalAdapter(R.layout.fragment_approval_item, fileList);
                                approvalAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                                    String fileName = fileList.get(position).getFileName();
                                    String fileUrl = fileList.get(position).getFileUrl();
                                    Intent intent = new Intent(mActivity, OfficeActivity.class);
                                    intent.putExtra("fileName", fileName);
                                    intent.putExtra("fileUrl", fileUrl);
                                    mActivity.startActivity(intent);
                                });
                                approvalAttachInfo.setAdapter(approvalAdapter);
                                approvalAdapter.notifyDataSetChanged();
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("暂无订单列表");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }
}
